package com.ztesoft.report.utils.seqcreate;

import java.util.HashMap;
import java.util.Map;

/**
 * 序列生成器
 *
 * @author lei.zuman
 * @date 17/12/12
 */
public class IdGenerator {
    private static Map<String, IdStep> idMap = new HashMap<String, IdStep>();

    public static long genLongValue(String tablename, String columnName) {
        return genLongValue(tablename, columnName,12);
    }
    public static int genIntValue(String tablename, String columnName) {
        return (int) genLongValue(tablename, columnName, 9);
    }

    private static synchronized long genLongValue(String tablename, String columnName, int len) {
        String key = tablename.toUpperCase() + "|" + columnName.toUpperCase();
        IdStep idStep = idMap.get(key);

        if (idStep == null || idStep.getStart() == idStep.getEnd()) {
            idStep = SpringContextUtils.getBean(InitIdServ.class).fetchNew(tablename, columnName,len);
            idMap.put(key, idStep);
        }
        idStep.setStart(idStep.getStart() + 1);
        return idStep.getStart() - 1;
    }



}
